package com.example.heartprotectionserver.controller;


import com.example.heartprotectionserver.model.Code;
import com.example.heartprotectionserver.model.Result;
import com.example.heartprotectionserver.model.User;
import com.example.heartprotectionserver.service.IUserService;
import com.example.heartprotectionserver.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张力文
 * @since 2023-07-12
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService iUserService;

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @GetMapping
    public Result login(String username,String password){
        Integer login = iUserService.login(username, password);
        if(login==1) return ResultUtil.getSuccess(null);
        else if(login==0) return ResultUtil.getError(Code.BUSINESS_USER_PASSWORD_ERR,"用户密码错误");
        else return ResultUtil.getError(Code.BUSINESS_USER_UNKNOW_ERR,"用户不存在");
    }

    @PostMapping
    public Result regist(@RequestBody User user){
        Integer regist = iUserService.regist(user);
        if(regist==-1){
            return ResultUtil.getError(Code.BUSINESS_USER_KNOW_ERR,"用户名已存在");
        } else if(regist==0){
            return ResultUtil.getError(Code.BUSINESS_USER_REGIST_ERR,"注册失败");
        } else {
            return ResultUtil.getSuccess(Code.SUCCESS);
        }
    }
}

